package org.yocastlong;

import com.alibaba.fastjson2.JSON;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.yocastlong.domain.mapper.UserMapper;
import org.yocastlong.domain.pojo.entity.User;

import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;

/**
 * @author 朱海龙
 * 2025/5/2 16:39
 */
public class Main {
    public static void main(String[] args) throws IOException {
        SqlSession sqlSession = null;
        try {
            InputStream       resourceAsStream  = Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
            sqlSession = sqlSessionFactory.openSession();
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            User       byId       = userMapper.getById("1");
            System.out.println(JSON.toJSONString(byId));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Optional.ofNullable(sqlSession)
                    .ifPresent(SqlSession::close);
        }


    }
}